package com.hehe.controller;

import com.hehe.powernode.model.Result;
import com.hehe.powernode.pojo.SysMenu;
import com.hehe.service.SysMenuService;
import com.hehe.util.AuthUtils;
import com.hehe.vo.LimitDate;
import com.hehe.vo.MenuAndPermsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Api(tags = "系统权限接口管理")
@RequestMapping("sys/menu")
@RestController
@Slf4j
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询登录用户的菜单和权限
     *
     * @return
     */
    @ApiOperation("查询登录用户的菜单和权限")
//    @PreAuthorize("hasAuthority('sys:user:delete')")
    @GetMapping("nav")
    public Result<MenuAndPermsVo> loadUserMenuAndPerms() {
        // 获取当前登录用户ID
        Long userId = AuthUtils.getLoginUserId();
        // 查询当前登录用户的权限集合
        Set<String> perms = AuthUtils.getPerms();
        // 根据用户ID查询菜单集合
        List<SysMenu> menus = sysMenuService.selectMenuByUserId(userId);
        // 创建菜单和权限集合对象
        MenuAndPermsVo menuAndPermsVo = new MenuAndPermsVo(menus, perms);
        return Result.success(menuAndPermsVo);
    }

    @ApiOperation("get测试")
    @GetMapping("/getTest")
    public Result<?> getTest(@RequestParam int size, @RequestParam int page, @RequestParam(required = false) String text) {
        //---/sys/menu/getTest?size=1&page=10&text=text
        log.info("getTest size:{},page:{},text:{}", size, page, text);
        //---getTest size:1,page:10,text:text
        return Result.success(null);
    }

    /**
     * ?
     *
     * @return
     */
    @ApiOperation("get多参测试")
    @GetMapping("/getTest2")
    public Result<LimitDate> get(LimitDate limitDate) {
        //sys/menu/getTest2?size=1&page=1&text=text
        log.info("limitDate limitDate:{}", limitDate);
        //(size=1, page=1, text=text)
        return Result.success(limitDate);
    }

    @ApiOperation("post测试")
    @PostMapping("/postTest")
    public Result<?> postTest(@RequestBody List<Integer> ids) {
        //postTest ids:[64, 65]
        log.info("postTest ids:{}", ids);
        return Result.success(null);
    }

    @ApiOperation("delete测试")
    @DeleteMapping("/deleteTest")
    public Result<?> deleteTest(@RequestParam List<Integer> ids) {
        //---/sys/menu/deleteTest?ids=12,13
        log.info("deleteTest ids:{}", ids);
        //---deleteTest ids:[12, 13]
        return Result.success(null);
    }

    @ApiOperation("put测试")
    @PutMapping("/putTest")
    public Result<?> putTest(@RequestParam List<Integer> ids) {
        //---/sys/menu/putTest?ids=12,13
        log.info("putTest ids:{}", ids);
        //---putTest ids:[12, 13]
        return Result.success(null);
    }
}